package net.kesumu.yushabuserver.utils;

import net.kesumu.yushabuserver.entity.FriendRequest;
import net.kesumu.yushabuserver.entity.User;
import net.kesumu.yushabuserver.entity.YushabuMessage;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * describe the message of the same format transmit by client and server 
 * @author Z 2014-12-19
 *
 */
public class JSONUtil {
    //self = This JSON contains the session information of that particular client. This will be the first json a client receives when it opens a sockets connection.
    //new = This JSON broadcasted to every client informing about the new client that is connected to socket server.
    //message = This contains the message sent by a client to server. Hence it will broadcasted to every client.
    //exit = The JSON informs every client about the client that is disconnected from the socket server.
    
    // flags to identify the kind of json response on client side
    public static final String  FLAG_EXIT = "exit",
                                FLAG_SELF = "self", 
                                FLAG_NEW = "new",
                                FLAG_LOGIN = "login", 
                                FLAG_REGISTER = "register",
                                FLAG_MESSAGE = "message", 
                                FLAG_USERINFO_GET = "userInfo_get",
                                FLAG_USERINFO_MODIFY = "userInfo_modify",
                                FLAG_FRIEND_SEARCH = "friend_search",
                                FLAG_FRIEND_REQUEST_SOURCE = "friend_request_source",
                                FLAG_FRIEND_REQUEST_DESTINATION = "friend_request_destination",
                                FLAG_FRIEND_REQUEST_ACCEPT = "friend_request_accept",
                                FLAG_FRIEND_REQUEST_REFUSE = "friend_request_refuse",
                                FLAG_FRIEND_LIST = "friend_list",
                                FLAG_MESSAGE_SINGLE_SOURCE = "message_single_source",
                                FLAG_MESSAGE_SINGLE_DESTINATION = "message_single_destination",
                                FLAG_MESSAGE_GROUP_SETUP = "message_group_setup",
                                FLAG_MESSAGE_GROUP_SOURCE = "message_group_source",
                                FLAG_MESSAGE_GROUP_DESTINATION = "message_group_destination",
                                FLAG_NOTIFICATION_FRIENDREQUEST = "notification_friend_request",
                                FLAG_NOTIFICATION_REQUEST = "notification_request";

    public JSONUtil() {
    }

    /**
     * Json when client needs it's own session details
     * */
    public String getClientDetailsJson(String sessionId, String message) {
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_SELF);
            jObj.put("sessionId", sessionId);
            jObj.put("message", message);

            json = jObj.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }

    /**
     * Json to notify all the clients about new person joined
     * */
    public String getNewClientJson(String sessionId, String name,
            String message, int onlineCount) {
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_NEW);
            jObj.put("name", name);
            jObj.put("sessionId", sessionId);
            jObj.put("message", message);
            jObj.put("onlineCount", onlineCount);

            json = jObj.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }

    /**
     * Json when the client exits the socket connection
     * */
    public String getClientExitJson(String sessionId, String name,
            String message, int onlineCount) {
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_EXIT);
            jObj.put("name", name);
            jObj.put("sessionId", sessionId);
            jObj.put("message", message);
            jObj.put("onlineCount", onlineCount);

            json = jObj.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }

    /**
     * JSON when message needs to be sent to all the clients
     * */
    public String getSendAllMessageJson(String sessionId, String fromName,
            String message) {
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_MESSAGE);
            jObj.put("sessionId", sessionId);
            jObj.put("name", fromName);
            jObj.put("message", message);

            json = jObj.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
    
    /**
     * JSON when user register to server
     */
    public String getRegisterJSON(String sessionId,User user) {
        
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_REGISTER);
            jObj.put("sessionId", sessionId);
            if(user == null) {
                jObj.put("result", "failed");
            }else {
                jObj.put("result", "success");
                jObj.put("userId", user.getUserId());
                jObj.put("email", user.getEmail());
                jObj.put("name", user.getName());
                jObj.put("yuyuChatId", user.getYuyuChatId());
                jObj.put("signature", user.getSignature());
                jObj.put("gender", user.getGender());
                jObj.put("area", user.getArea());
            }
            json = jObj.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
    
    /**
     * JSON when user register to server
     */
    public String getLoginJSON(String sessionId,User user) {
        
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_LOGIN);
            jObj.put("sessionId", sessionId);
            if(user == null) {
                jObj.put("result", "failed");
            }else {
                jObj.put("result", "success");
                jObj.put("userId", user.getUserId());
                jObj.put("email", user.getEmail());
                jObj.put("name", user.getName());
                jObj.put("yuyuChatId", user.getYuyuChatId());
                jObj.put("signature", user.getSignature());
                jObj.put("gender", user.getGender());
                jObj.put("area", user.getArea());
            }
            json = jObj.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
    
    public String getUserInfoJSON(String sessionId,User user) {
        
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_USERINFO_GET);
            jObj.put("sessionId", sessionId);
            if(user == null) {
                jObj.put("result", "failed");
            }else {
                jObj.put("result", "success");
                jObj.put("userId", user.getUserId());
                jObj.put("userName", user.getName());
                jObj.put("yuyuChatId", user.getYuyuChatId());
                jObj.put("userSignature", user.getSignature());
                jObj.put("userGender", user.getGender());
                jObj.put("userArea", user.getArea());
            }
            json = jObj.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
    
    public String getUserInfoModifyResultJSON(String sessionId, String result) {
        
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_USERINFO_MODIFY);
            jObj.put("sessionId", sessionId);
            jObj.put("result", result);

            json = jObj.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
    
    public String getFriendSearchResultJSON(String sessionId, User user) {
        
        String json = null;

        
        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_FRIEND_SEARCH);
            jObj.put("sessionId", sessionId);
            if(user == null) {
                jObj.put("result", "failed");
            }else {
                jObj.put("result", "success");
                jObj.put("userId", user.getUserId());
                jObj.put("userName", user.getName()); 
                jObj.put("yuyuChatId", user.getYuyuChatId()); 
            }
            json = jObj.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
    
    public String getFriendRequestSourceResultJSON(String sessionId, String result) {
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_FRIEND_REQUEST_SOURCE);
            jObj.put("sessionId", sessionId);
            jObj.put("result", result);

            json = jObj.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
    
    public String getFriendRequestDestinationResultJSON(String sessionId, FriendRequest friendRequest) {
        
        String json = null;

        
        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_FRIEND_REQUEST_DESTINATION);
            jObj.put("sessionId", sessionId);
            if(friendRequest == null) {
                jObj.put("result", "failed");
            }else {
                jObj.put("result", "success");
                jObj.put("userId", friendRequest.getUserId());
                jObj.put("friendId", friendRequest.getFriendId());
                jObj.put("message", friendRequest.getMessage());
                jObj.put("state", friendRequest.getState());
                jObj.put("userName", friendRequest.getUserName()); 
                jObj.put("yuyuChatId", friendRequest.getUserYuYuChatId()); 
            }
            json = jObj.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
    
    public String getAcceptFriendRequestResultJSON(String sessionId, String result) {
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_FRIEND_REQUEST_ACCEPT);
            jObj.put("sessionId", sessionId);
            jObj.put("result", result);

            json = jObj.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
    
    public String getRefuseFriendRequestResultJSON(String sessionId, String result) {
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_FRIEND_REQUEST_ACCEPT);
            jObj.put("sessionId", sessionId);
            jObj.put("result", result);

            json = jObj.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
    
    public String getFriendListResultJSON(String sessionId, User user) {
        
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_FRIEND_LIST);
            jObj.put("sessionId", sessionId);
            if(user == null) {
                jObj.put("result", "failed");
            }else {
                jObj.put("result", "success");
                jObj.put("userId", user.getUserId());
                jObj.put("userName", user.getName()); 
                jObj.put("yuyuChatId", user.getYuyuChatId()); 
            }
            json = jObj.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
    
    public String getSendMessageSingleResultJSON(String sessionId, String result
                                                 , YushabuMessage yushabuMessage) {
        String json = null;

        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_MESSAGE_SINGLE_SOURCE);
            jObj.put("sessionId", sessionId);
            jObj.put("result", result);
            if(yushabuMessage != null) {
                jObj.put("messageId", yushabuMessage.getMessageId());
                jObj.put("content", yushabuMessage.getContent());
                jObj.put("sendTime", yushabuMessage.getSendTime().toString());
                jObj.put("userId", yushabuMessage.getUserId());
                jObj.put("friendId", yushabuMessage.getFriendUserId());
            }
            json = jObj.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }
    
    public String getServerSendMessageSingleJSON(String sessionId,  YushabuMessage yushabuMessage) {
        String json = null;
        
        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_MESSAGE_SINGLE_DESTINATION);
            jObj.put("sessionId", sessionId);
            jObj.put("chatId", yushabuMessage.getSessionId());
            jObj.put("messageId", yushabuMessage.getMessageId());
            jObj.put("content", yushabuMessage.getContent());
            jObj.put("sendTime", yushabuMessage.getSendTime().toString());
            jObj.put("userId", yushabuMessage.getUserId());
            jObj.put("friendId", yushabuMessage.getFriendUserId());
            json = jObj.toString();
        } catch (JSONException e) {
        e.printStackTrace();
        }
        return json;
    }
    
    public String getServerSetupMessageGroupJSON(String sessionId,  String result) {
        String json = null;
        
        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_MESSAGE_GROUP_SETUP);
            jObj.put("sessionId", sessionId);
            jObj.put("result", result);
            json = jObj.toString();
        } catch (JSONException e) {
        e.printStackTrace();
        }
        return json;
    }
    
    /*public String getServerSendMessageGroupJSON(String sessionId,  YushabuMessage yushabuMessage) {
        String json = null;
        
        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_MESSAGE_GROUP_DESTINATION);
            jObj.put("sessionId", sessionId);
            jObj.put("messageId", yushabuMessage.getMessageId());
            jObj.put("content", yushabuMessage.getContent());
            jObj.put("sendTime", yushabuMessage.getSendTime().toString());
            jObj.put("userId", yushabuMessage.getUserId());
            jObj.put("friendId", yushabuMessage.getFriendUserId());
            json = jObj.toString();
        } catch (JSONException e) {
        e.printStackTrace();
        }
        return json;
    }*/
    
    public String getServerNotificationFriendRequestJSON(String sessionId, String unreadedFriendRequestNumber) {
        String json = null;
        
        try {
            JSONObject jObj = new JSONObject();
            jObj.put("flag", FLAG_NOTIFICATION_FRIENDREQUEST);
            jObj.put("sessionId", sessionId);
            jObj.put("unreadedFriendRequestNumber", unreadedFriendRequestNumber);
            json = jObj.toString();
        } catch (JSONException e) {
        e.printStackTrace();
        }
        return json;
    }
    
}
